<?php

namespace app\models;

use Yii;
use app\models\User;
use yii\helpers\Html;

/**
 * This is the model class for table "{{%news}}".
 *
 * @property int $id
 * @property string $title 资讯标题
 * @property string $author 资讯作者
 * @property string $email 联系邮箱
 * @property int $category 资讯类别
 * @property string $brief_desc 资讯简要描述
 * @property string $desc 资讯完整内容
 * @property string $cover 上传封面图标
 * @property string $label 资讯标签
 * @property int $created_time 资讯创建时间
 * @property int $updated_time 资讯更新时间
 */
class News extends \yii\db\ActiveRecord
{   
    const CHECK_STATUS_WATING = 0;//待审核
    const CHECK_STATUS_NOT_PASS = 1;//不通过
    const CHECK_STATUS_PASS = 2;//通过

    public static function getCheckStatus($status=null, $field=null){
        $arr = [
            self::CHECK_STATUS_WATING => ['name' => '待审核', 'class' =>'label-warning btn-xs'],
            self::CHECK_STATUS_NOT_PASS => ['name' => '不通过', 'class' =>'label-danger btn-xs'],
            self::CHECK_STATUS_PASS => ['name' => '通过', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return $arr;
    }

    //获取
    public static function getCheckStatusKeyPair($field='name'){
        $data = self::getCheckStatus();

        $retuen_arr = [];
        foreach ($data as $key => $value) {
            $retuen_arr[$key] = $value[$field];
        }
        return $retuen_arr;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%news}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // [['category'], 'integer'],
            // [['desc'], 'string'],
            // [['title', 'email', 'brief_desc', 'cover'], 'string', 'max' => 255],
            // [['author', 'label'], 'string', 'max' => 50],
            // [['created_time', 'updated_time'],'safe'],
            [['status'], 'integer'],
            [['opt_note'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'title' => Yii::t('app', '资讯标题'),
            'author' => Yii::t('app', '资讯作者'),
            'email' => Yii::t('app', '联系邮箱'),
            'category' => Yii::t('app', '资讯类别'),
            'brief_desc' => Yii::t('app', '资讯简要描述'),
            'desc' => Yii::t('app', '资讯完整内容'),
            'cover' => Yii::t('app', '上传封面图标'),
            'label' => Yii::t('app', '资讯标签'),
            'created_time' => Yii::t('app', '资讯创建时间'),
            'updated_time' => Yii::t('app', '资讯更新时间'),
            'status' => Yii::t('app', '审核状态'),
            'u.nickname' => Yii::t('app', '用户名'),
            'opt_note' => Yii::t('app', '备注'),
        ];
    }

    public function beforeSave($insert)  
    {  
        if(parent::beforeSave($insert))  
        {  
            if($this->isNewRecord)  
            {  
                $this->created_time=time();  
                $this->updated_time=time(); 
            } else{
                $this->updated_time=time(); 
            }
            return true;  
        }  
        else  
            return false;  
    } 

    public function getPictrues($json_picture, $width='100', $height='100'){
        return '<img src="'.$json_picture.'" width="'.$width.'px" height="'.$height.'px">';
    }

     /**  
     * This is invoked after the record is saved.  
     */  
    // public function afterSave($insert, $changedAttributes)  
    // {  
    //     parent::afterSave($insert, $changedAttributes);  
    //     Tag::updateFrequency($this->_oldTags, $this->tags,strtolower($this->getClassName()));  
    // }  
    // 
    
    public function getUser()
    {
        return $this->hasOne(User::className(), ['uid' => 'author']);
    }

}
